using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CChapterPictureList : List<CChapterPicture>
    {
        #region Constructors
        //Basic constructor
        public CChapterPictureList() : base() {}
       
        //More efficient memory-allocation if size is known
        public CChapterPictureList(int capacity) : base(capacity) {}
        
        //Shares the index (if its already been computed)
        public CChapterPictureList(CChapterPictureList list) : base(list.Count)
        {
            foreach (CChapterPicture i in list)
                base.Add(i);
        }

        //Generic list (eg. from paging control), have to assume type
        public CChapterPictureList(IList list) : base(list.Count)
        {
            foreach (CChapterPicture i in list)
                base.Add(i);
        }
        #endregion
        
        #region Top/Bottom/Page
        public CChapterPictureList Top(int count)
        {
            if (count >= this.Count)
                return this;
            return Page(count, 0); 
        }
        public CChapterPictureList Bottom(int count)   
        {
            if (count > this.Count)
                count = this.Count;
            return new CChapterPictureList(this.GetRange(this.Count - count - 1, count));
        }
        public CChapterPictureList Page(int pageSize, int pageIndex)
        {
            return new CChapterPictureList( CUtilities.Page(this, pageSize, pageIndex) );
        }
        #endregion

        #region BulkEditLogic
        public bool HaveSameValue(string propertyName)               { return CReflection.HaveSameValue(this, propertyName); }
        public void SetSameValue( string propertyName, object value) {        CReflection.SetSameValue( this, propertyName, value); }
        #endregion

        #region SortBy
        //Public
        public CChapterPictureList SortBy(string propertyName) { return SortBy(propertyName, false); }
        public CChapterPictureList SortBy(string propertyName, bool descending)
        {
            CChapterPictureList copy = new CChapterPictureList(this);
            if (this.Count == 0)    return copy;
            copy.Sort(new CChapterPictureList_SortBy(propertyName, descending, this));
            return copy;
        }
        //Private 
        private class CChapterPictureList_SortBy : CReflection.GenericSortBy, IComparer<CChapterPicture>
        {
            public CChapterPictureList_SortBy(string propertyName, bool descending, IList list) : base(propertyName, descending, list) { }
            public int Compare(CChapterPicture x, CChapterPicture y) { return base.Compare(x, y); }
        }
        #endregion

        #region SaveAll/DeleteAll
        //Use default connection (may be overridden in base class)
        public void SaveAll()   {   if (this.Count > 0) { SaveAll(  this[0].DataSrc);    }    }
        public void DeleteAll() {   if (this.Count > 0) { DeleteAll(this[0].DataSrc);    }    }

        //Use connection supplied
        public void SaveAll(  CDataSrc dataSrc) {   dataSrc.BulkSave(this);    }
        public void DeleteAll(CDataSrc dataSrc) {   dataSrc.BulkDelete(this);  }

        //Use transaction supplied
        public void SaveAll(  IDbTransaction txOrNull)    {   foreach (CChapterPicture i in this) {   i.Save(  txOrNull);   }   }
        public void DeleteAll(IDbTransaction txOrNull)    {   foreach (CChapterPicture i in this) {   i.Delete(txOrNull);   }   }

        //Use a specified isolation level
        public void SaveAll(  IsolationLevel txIsolationLevel)   {   if (this.Count > 0) { SaveAll(  this[0].DataSrc, txIsolationLevel);  }    }
        public void DeleteAll(IsolationLevel txIsolationLevel)   {   if (this.Count > 0) { DeleteAll(this[0].DataSrc, txIsolationLevel);  }    }

        //Use a specified connection and isolation level
        public void SaveAll(  CDataSrc dataSrc, IsolationLevel txIsolationLevel)   {   dataSrc.BulkSave(  this, txIsolationLevel);  }
        public void DeleteAll(CDataSrc dataSrc, IsolationLevel txIsolationLevel)   {   dataSrc.BulkDelete(this, txIsolationLevel);  }
        #endregion

        #region List of Ids
        List<int> _ids;
        public List<int> Ids
        {
            get
            {
                if (null == _ids)
                {
                    List<int> ids = new List<int>(this.Count);
                    foreach (CChapterPicture i in this)
                        ids.Add(i.PictureId);
                    _ids = ids;
                }
                return _ids;
            }
        }
        public CChapterPictureList GetByIds(List<int> ids)
        {
            CChapterPictureList list = new CChapterPictureList(ids.Count);
            foreach (int id in ids)
                if (null != GetById(id))
                    list.Add(GetById(id));
            return list;
        }
        #endregion
        
        #region Cache-Control
        //Supplementary List Overloads
        public void Add(   IList<CChapterPicture> itemsToAdd)    {   foreach (CChapterPicture i in itemsToAdd)   { Add(   i); }   }
        public void Remove(IList<CChapterPicture> itemsToRemove) {   foreach (CChapterPicture i in itemsToRemove){ Remove(i); }   }
        #endregion
        
        #region Main Index (on PictureId)
        public CChapterPicture GetById(int pictureId)
        {
            CChapterPicture c = null;
            Index.TryGetValue(pictureId, out c);
            return c;
        }
        [NonSerialized]
        private Dictionary<int,CChapterPicture> _index;
        private Dictionary<int,CChapterPicture> Index
        {
            get
            {
                if (null != _index)
                    if (_index.Count == this.Count)
                        return _index;

                    var temp = new Dictionary<int,CChapterPicture>(this.Count);
                        foreach (CChapterPicture i in this) 
                            temp[i.PictureId] = i;
				_index = temp;
                return _index;
            }
        }
        #endregion
            
        #region Foreign-Key Indices (Subsets)
        //Index by PictureChapterId
        public CChapterPictureList GetByChapterId(int chapterId)
        {
            CChapterPictureList temp = null;
            if (! IndexByChapterId.TryGetValue(chapterId, out temp))
            {
                temp = new CChapterPictureList();
                IndexByChapterId[chapterId] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<int, CChapterPictureList> _indexByChapterId;
        private Dictionary<int, CChapterPictureList> IndexByChapterId
        {
            get
            {
                if (null == _indexByChapterId)
                {
                    Dictionary<int, CChapterPictureList> index = new Dictionary<int, CChapterPictureList>();
                    CChapterPictureList temp = null;
                    foreach (CChapterPicture i in this)
                    {
                        if (! index.TryGetValue(i.PictureChapterId, out temp))
                        {
                            temp = new CChapterPictureList();
                            index[i.PictureChapterId] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByChapterId = index;
                }
                return _indexByChapterId;
            }
        }
        //Index by PicturePhotoMD5
        public CChapterPictureList GetByPhotoMD5(Guid photoMD5)
        {
            CChapterPictureList temp = null;
            if (! IndexByPhotoMD5.TryGetValue(photoMD5, out temp))
            {
                temp = new CChapterPictureList();
                IndexByPhotoMD5[photoMD5] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<Guid, CChapterPictureList> _indexByPhotoMD5;
        private Dictionary<Guid, CChapterPictureList> IndexByPhotoMD5
        {
            get
            {
                if (null == _indexByPhotoMD5)
                {
                    Dictionary<Guid, CChapterPictureList> index = new Dictionary<Guid, CChapterPictureList>();
                    CChapterPictureList temp = null;
                    foreach (CChapterPicture i in this)
                    {
                        if (! index.TryGetValue(i.PicturePhotoMD5, out temp))
                        {
                            temp = new CChapterPictureList();
                            index[i.PicturePhotoMD5] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByPhotoMD5 = index;
                }
                return _indexByPhotoMD5;
            }
        }
        #endregion

        #region Move Up/Down
        public void MoveUp(  CChapterPicture s) { Move(s, -1); }
        public void MoveDown(CChapterPicture s) { Move(s,  1); }
        public void SortAlphabetically()
        {
            foreach (CChapterPicture i in this)
                i.PictureSortOrder = 0;
            this.SaveAll();
            CChapterPicture.Cache = null;
        }

        //Private 
        public void Move(CChapterPicture s, int change)
        { 
            if (s == null)
                return;

            int index = this.IndexOf(s) + change;
            if (index < 0) return;
            
            if (index > this.Count - 1)
                return;
            
            //Modify a copy of the array for threadsafety 
            CChapterPictureList dd = new CChapterPictureList(this.Count); 
            dd.AddRange(this);
            dd.Remove(s);
            dd.Insert(index, s);
            dd.ResetOrdinals();
            
            this.SaveAll();
            CChapterPicture.Cache = null;
        }
        public void ResetOrdinals()
        {
            for (int i = 0; i <= this.Count - 1; i++)
                this[i].PictureSortOrder = i;
        }
        #endregion

    }
}
